package py.com.sole.solesys;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.SelectEvent;
import py.com.sole.dao.DaoFactory;
import py.com.sole.dto.VentaDTO;
import py.com.sole.dto.VentaDetalleDTO;
import py.com.sole.model.Articulo;
import py.com.sole.model.Persona;
import py.com.sole.util.TipoPago;

@ManagedBean
@SessionScoped
public class VentaBean implements Serializable {

  /**
   *
   */
  private static final long serialVersionUID = -4873747667246098929L;

  private VentaDTO venta;
  private Articulo articuloSelected;
  private int cantidadSelected;
  private Double montoPagar;
  private String observacion;
  private TipoPago tipoPagoSelected;
  private py.com.sole.model.Persona personaSelected;
  private List<py.com.sole.model.Persona> personas;

  private VentaDetalleDTO ventaSelected;
  private List<TipoPago> tiposPagos;
  private Double totalAPagar;
  private boolean bloquearBotones = true;

  public void clean() {

    totalAPagar = 0D;
    montoPagar = 0D;
    cantidadSelected = 0;
    articuloSelected = null;
    observacion = null;
    tipoPagoSelected = null;
    personaSelected = null;

    tiposPagos = new ArrayList<TipoPago>();
    venta = new VentaDTO();
    venta.setVentaDetalles(new ArrayList<VentaDetalleDTO>());
    this.bloquearBotones = true;
    //RequestContext.getCurrentInstance().update("dtRol");
  }

  @PostConstruct
  private void init() {
    totalAPagar = 0D;
    montoPagar = 0D;
    cantidadSelected = 0;
    tiposPagos = new ArrayList<TipoPago>();
    venta = new VentaDTO();
    venta.setVentaDetalles(new ArrayList<VentaDetalleDTO>());
  }

  public void addArticulos() {

    VentaDetalleDTO v = new VentaDetalleDTO();
    v.setArticulo(articuloSelected);
    v.setCantidad(cantidadSelected);
    v.setTotalVenta(articuloSelected.getPrecioVenta() * cantidadSelected);
    totalAPagar = totalAPagar + v.getTotalVenta();
    venta.getVentaDetalles().add(v);

  }

  public void cobrar() {
    venta.setFecha(new Date());
    venta.setPersona(personaSelected);
    venta.setMontoPagar(montoPagar);
    venta.setTipoPago(tipoPagoSelected.name());
    venta.setObservacion(observacion);

    //controler 
    //cuentas.add(VentaBean, venta)
  }

  public void onRowSelect(SelectEvent event) {

  }

  public List<String> completeText(String query) {
    List<String> results = new ArrayList<String>();
    List<Persona> resPersona = DaoFactory.getPersona().filtrarPersonas(query);
    System.out.println("CompleteText:" + query);
    if (null != resPersona) {
      for (Persona per : resPersona) {
        results.add(per.getNombre() + " " + per.getApellido());
        System.out.println("Resultados:" + per.getNombre() + " " + per.getApellido());
      }

    } else {
      System.out.println("CompleteText: no ");
    }

    return results;
  }

  public boolean isBloquearBotones() {
    return bloquearBotones;
  }

  public void setBloquearBotones(boolean bloquearBotones) {
    this.bloquearBotones = bloquearBotones;
  }

  public VentaDetalleDTO getVentaSelected() {
    return ventaSelected;
  }

  public void setVentaSelected(VentaDetalleDTO ventaSelected) {
    this.ventaSelected = ventaSelected;
  }

  public List<TipoPago> getTiposPagos() {
    if (tiposPagos == null || tiposPagos.isEmpty()) {
      tiposPagos = new ArrayList<TipoPago>();
      tiposPagos.addAll(Arrays.asList(TipoPago.values()));
    }
    return tiposPagos;
  }

  public List<py.com.sole.model.Persona> getPersonas() {
    if (personas == null || personas.isEmpty()) {
      personas = DaoFactory.getPersona().getPersonas();
    }
    return personas;
  }

  public void setPersonas(List<py.com.sole.model.Persona> personas) {
    this.personas = personas;
  }

  public void setTiposPagos(List<TipoPago> tiposPagos) {
    this.tiposPagos = tiposPagos;
  }

  public String getObservacion() {
    return observacion;
  }

  public void setObservacion(String observacion) {
    this.observacion = observacion;
  }

  public Articulo getArticuloSelected() {
    return articuloSelected;
  }

  public void setArticuloSelected(Articulo articuloSelected) {
    this.articuloSelected = articuloSelected;
  }

  public int getCantidadSelected() {
    return cantidadSelected;
  }

  public void setCantidadSelected(int cantidadSelected) {
    this.cantidadSelected = cantidadSelected;
  }

  public VentaDTO getVenta() {
    return venta;
  }

  public void setVenta(VentaDTO venta) {
    this.venta = venta;
  }

  public Double getMontoPagar() {
    return montoPagar;
  }

  public void setMontoPagar(Double montoPagar) {
    this.montoPagar = montoPagar;
  }

  public TipoPago getTipoPagoSelected() {
    return tipoPagoSelected;
  }

  public void setTipoPagoSelected(TipoPago tipoPagoSelected) {
    this.tipoPagoSelected = tipoPagoSelected;
  }

  public Persona getPersonaSelected() {
    return personaSelected;
  }

  public void setPersonaSelected(Persona personaSelected) {
    this.personaSelected = personaSelected;
  }

  public Double getTotalAPagar() {
    return totalAPagar;
  }

  public void setTotalAPagar(Double totalAPagar) {
    this.totalAPagar = totalAPagar;
  }

}
